package search

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat/global"
	"wechat/handle/model"
	"wechat/handle/request"
)

//添加黑名单

func AddBlack(c *gin.Context) {
	var form request.AddBlack
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var black model.Black
	if err := black.VeryBlack(global.DB, form.UserId, form.FriendId); err == nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "好友已被拉黑,请勿重复操作",
		})
		return
	}

	black = model.Black{
		UserId:   form.UserId,
		FriendId: form.FriendId,
	}

	if err := black.AddBlack(global.DB); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "黑名单添加失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "黑名单添加成功",
	})

}

// 黑名单列表
func ListBlack(c *gin.Context) {
	var form request.ListBlack
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var black model.Black

	listFriend, err := black.ListBlack(global.DB, form.UserId)
	if err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查询失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "黑名单列表查询成功",
		"Data": map[string]interface{}{
			"list": listFriend,
		},
	})

}
